/*
 * camera_ptz_switch.cpp
 *
 *  Created on: Mar 31, 2010
 *      Author: root
 *
 */

#include "control_center.hpp"

cmd_exec_over_code control_center::switch_camera(t_matrix_input *matrix_input,t_matrix_output *matrix_output)
{
   my_printf("switch camera:%s to monitor:%s",matrix_input->name,matrix_output->name);
  //call device
   return CMD_EXEC_OK;
}

cmd_exec_over_code control_center::switch_camera(t_matrix_input *matrix_input,t_matrix_output *matrix_output_trun,t_matrix_input *matrix_input_trun,t_matrix_output *matrix_output)
{
   my_printf("switch camera:%s to  trunks:%s,and from trunks:%s to monitor:%s",matrix_input->name,matrix_output_trun->name,matrix_input_trun->name,matrix_output->name);
   return CMD_EXEC_OK;
}

u32 control_center::getusergrouplevel(u32 userid)
{
	struct list_head *pos_usergroup;
	t_usergroup *user_group;

	struct list_head *pos_user;
	t_user *user;

	  list_for_each(pos_user,&user_list)
	  {
		  user=list_entry(pos_user,struct t_user,list);
		  if ((user->code )==userid)
		  {
			  list_for_each(pos_usergroup,&usergroup_list)
		      {
				  user_group=list_entry(pos_usergroup,struct t_usergroup,list);
				  if ((user_group->id)==(user->groupid))
				  {
                        return user_group->level;
				  }
		      }
		  }
	  }
	  return 0;
}


cmd_exec_over_code control_center::switch_matrix_two(u32 userid,u32 monitor,u32 camera,t_matrix_input *matrix_input,t_matrix_output *matrix_output)
{
	struct list_head *pos_camera_trun;
	t_matrix_input *matrix_input_trun;

	struct list_head *pos_monitor_trun;
	t_matrix_output *matrix_output_trun;

	t_matrix_input *select__input_trun=NULL;
	t_matrix_output *select_output_trun=NULL;


    bool have_trunk=false;
	u32 grouplevel_new=0,grouplevel_old=0;

	//find used turn
	  list_for_each(pos_camera_trun,&camera_list)
	  {
		  matrix_input_trun=list_entry(pos_camera_trun,struct t_matrix_input,list);
         if ((matrix_input_trun->inttype)==INT_TYPE_PRIVATE_TRUN)
         {
      	   my_printf("Switch  find one trunk\n");
           list_for_each(pos_monitor_trun,&monitor_list)
      	  {
        	  matrix_output_trun=list_entry(pos_monitor_trun,struct t_matrix_output,list);
          	  if ((matrix_output_trun->outtype)==OUT_TYPE_PRIVATE_TRUN)
          	  {
          		  if (( (matrix_output_trun->output_no)==(matrix_input_trun->input_no)) && ( (matrix_output->matrixid) == (matrix_output_trun->matrixid ) ))
          		  {
                      if (matrix_output_trun->selectcameraid==camera)
                      {
                           //call two
              			   my_printf("find used trunk\n");
                    	   return switch_camera(matrix_input,matrix_output_trun,matrix_input_trun,matrix_output);
                      }

          		  }
          	  }
      	  }
        }
	  }

		//find free turn
		  list_for_each(pos_camera_trun,&camera_list)
		  {
			  matrix_input_trun=list_entry(pos_camera_trun,struct t_matrix_input,list);
	          my_printf("each of camera id=%d,name=%s\n",matrix_input_trun->id,matrix_input_trun->name);
	         if ((matrix_input_trun->inttype)==INT_TYPE_PRIVATE_TRUN)
	         {
	      	   my_printf("Switch cmd find one trunk\n");
	           list_for_each(pos_monitor_trun,&monitor_list)
	      	  {
	        	  matrix_output_trun=list_entry(pos_monitor_trun,struct t_matrix_output,list);
	          	  my_printf("each of matrix_output_trun id=%d,name=%s\n",matrix_output_trun->output_no,matrix_output_trun->name);
	          	  if ((matrix_output_trun->outtype)==OUT_TYPE_PRIVATE_TRUN)
	          	  {
	          		  my_printf("find matrix_output_trun,%s\n",matrix_output_trun->name);
	                    //test in one matrix;
	          		 if (( (matrix_output_trun->output_no)==(matrix_input_trun->input_no)) && ( (matrix_output->matrixid) == (matrix_output_trun->matrixid ) ))
	          		  {
	                      if (matrix_output_trun->selectcameraid==0)
	                      {
	                           //call two
		          			  my_printf("find free trunk\n");
	                    	  return switch_camera(matrix_input,matrix_output_trun,matrix_input_trun,matrix_output);
	                      }
	                        //call encode xml make send data.
	          		  }
	          	  }
	      	  }
	        }
		  }

			  //turn loot
		      //give new user level;
		     grouplevel_new=getusergrouplevel(userid);

			  list_for_each(pos_camera_trun,&camera_list)
			  {
				  matrix_input_trun=list_entry(pos_camera_trun,struct t_matrix_input,list);
		          my_printf("each of camera id=%d,name=%s\n",matrix_input_trun->id,matrix_input_trun->name);
		         if ((matrix_input_trun->inttype)==INT_TYPE_PRIVATE_TRUN)
		         {
		      	   my_printf("Switch cmd find one trunk\n");
		           list_for_each(pos_monitor_trun,&monitor_list)
		      	  {
		        	  matrix_output_trun=list_entry(pos_monitor_trun,struct t_matrix_output,list);
		          	  my_printf("each of matrix_output_trun id=%d,name=%s\n",matrix_output_trun->output_no,matrix_output_trun->name);
		          	  if ((matrix_output_trun->outtype)==OUT_TYPE_PRIVATE_TRUN)
		          	  {
		          		  my_printf("find matrix_output_trun,%s\n",matrix_output_trun->name);
		                    //test in one matrix;
		          		 if (( (matrix_output_trun->output_no)==(matrix_input_trun->input_no)) && ( (matrix_output->matrixid) == (matrix_output_trun->matrixid ) ))
		          		  {
		                         //call two
			          		    my_printf("find used trunk\n");
			          		    have_trunk=true;
                                //give used level;
			          			grouplevel_old=getusergrouplevel(matrix_output_trun->useduser);
			          			if (grouplevel_old>grouplevel_new)
			          				{
			          				  select_output_trun=matrix_output_trun;
			          				  select__input_trun=matrix_input_trun;
			          				}
		          		  }
		          	  }
		      	  }
		        }
			  }

			 if(select_output_trun!=NULL)//not null
			 {
				 my_printf("find loot trunk\n");
				 return switch_camera(matrix_input,select_output_trun,select__input_trun,matrix_output);
			 };

			 if (have_trunk) return CMD_EXEC_NO_FREE_TURN;
			 else return CMD_EXEC_NOT_FIND_PATH;
}

/*
 * camera only ptzcontrol.
 */

cmd_exec_over_code control_center::camera_ptz_do(u32 userid,camera_control_switch_cmd command,u32 monitor,u32 camera,u32 parameter)
{

}



cmd_exec_over_code control_center::camera_ptz_switch(u32 userid,camera_control_switch_cmd command,u32 monitor,u32 camera,u32 parameter)
{
	camera_control_switch_cmd exchange_command;

	struct list_head *pos_camera;
	t_matrix_input *matrix_input;

	struct list_head *pos_monitor;
	t_matrix_output *matrix_output;


	struct list_head *pos_camera_control;
	t_camera_control *camera_control;

	struct list_head *pos_camera_control_exchange;
	t_camera_control_exchange *camera_control_exchange;

	switch (command)
	{
	  case CAMERA_SWITCH:    //it's switch.
	  {
		  list_for_each(pos_camera,&camera_list)
		  {
			  matrix_input=list_entry(pos_camera,struct t_matrix_input,list);
              my_printf("each of camera id=%d,name=%s\n",matrix_input->id,matrix_input->name);
              if ((matrix_input->input_no)==camera)
              {
            	  my_printf("Switch cmd find camera\n");
                  list_for_each(pos_monitor,&monitor_list)
            	  {
                	  matrix_output=list_entry(pos_monitor,struct t_matrix_output,list);
                	  my_printf("each of monitor id=%d,name=%s\n",matrix_output->output_no,matrix_output->name);
                	  if ((matrix_output->output_no)==monitor)
                	  {
                		  my_printf("find monitor,%s\n",matrix_output->name);
                          //test in one matrix;
                		  if ((matrix_output->matrixid)==(matrix_input->matrixid))
                		  {
                			  my_printf("camera and monitor in one matrix\n");
                			  return switch_camera(matrix_input,matrix_output);
                		  }
                		  else
                		  {
                			  my_printf("camera and monitor out in one matrix\n");
                			  return switch_matrix_two(userid,monitor,camera,matrix_input,matrix_output);
                		  }

                	  }
            	  }

                  break;
              }
		  }
           break;
	  }
	  case PTZ_UP_START:
	  {
		  list_for_each(pos_camera_control,&camerar_control_list)
		  {
			  camera_control=list_entry(pos_camera_control,struct t_camera_control,list);
			  if ((camera_control->camera_id)==camera)
			  {
				  my_printf("find camera %d's ptz\n",camera);
                 //find exchange of control.
				  exchange_command=command;
                  list_for_each(pos_camera_control_exchange,&camerar_control_exchange_list)
                  {
                	  camera_control_exchange=list_entry(pos_camera_control_exchange,struct t_camera_control_exchange,list);
                	  if ( (camera_control_exchange->camera_control_id==camera_control->id) && (camera_control_exchange->from==command) )
                	  {
                		  my_printf("camera %d's exchange ptz\n",camera);
                		  exchange_command=camera_control_exchange->to;
                		  break;
                	  }
                  }
                  //call camera ptz.
                  return camera_ptz_do(userid,exchange_command,monitor,camera,parameter);
			  }
		  }
	      break;
	  }
	  default:
	  {
		  return CMD_EXEC_COMMAND_UNKNOW;
	  }
	}
	return CMD_EXEC_COMMAND_UNKNOW;
}




